import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.thread
import kotlin.concurrent.withLock

fun main() {
    thread { sellTicket() }
    thread { sellTicket() }
    thread { sellTicket() }
    thread { sellTicket() }
}

private var ticketNum = 100
private val lock = ReentrantLock()

//@Synchronized
private fun sellTicket() {
    lock.withLock {
        while (ticketNum > 0) {
            Thread.sleep(50)
            println(Thread.currentThread().name + ": " + ticketNum--)
        }
    }
}